using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MinoChat.Services.Interfaces;
using MinoChat.Views;
using MinoChat.Views.Pages;

namespace MinoChat.Services;

public class ApplicationHostService : IHostedService
{
    private readonly IServiceProvider _serviceProvider;
    public ApplicationHostService(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }
    public async Task StartAsync(CancellationToken cancellationToken)
    {
        await HandleActivationAsync();
    }
    public async Task StopAsync(CancellationToken cancellationToken)
    {
        await Task.CompletedTask;
    }
    private async Task HandleActivationAsync()
    {
        // 创建并显示主窗口（主题已在 MainWindow 构造函数中应用）
        var mainWindow = _serviceProvider.GetRequiredService<MainWindow>();
        mainWindow!.Show();

        // 导航到聊天页面（此时窗口已显示，ContentControl已初始化）
        _serviceProvider.GetRequiredService<INavigationService>().NavigateTo<ChatPage>();
        await Task.CompletedTask;
    }
}